package cst.a225xm.startproject225;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import cst.a225xm.startproject225.bean.WeatherInfo;
import cst.a225xm.startproject225.listener.WeatherListener;
import cst.a225xm.startproject225.model.Weather51Model;
import cst.a225xm.startproject225.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,WeatherListener{

    private EditText cityid;
    private TextView city,citynumber,temp,sd,wd,ws;
    private Button button1,button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cityid=(EditText) findViewById(R.id.cityid);
        city=(TextView) findViewById(R.id.city);
        citynumber=(TextView) findViewById(R.id.citynumber);
        temp=(TextView) findViewById(R.id.temp);
        sd=(TextView) findViewById(R.id.sd);
        wd=(TextView) findViewById(R.id.wd);
        ws=(TextView) findViewById(R.id.ws);

        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:
                WeatherModel weatherModel=new WeatherModel();
                weatherModel.getWeather(cityid.getText().toString(),this);
                break;
            case R.id.button2:
                Weather51Model weather51Model=new Weather51Model();
                weather51Model.getWeather(cityid.getText().toString(),this);
                break;
            default:
                break;
        }
    }

    @Override
    public void onResponse(WeatherInfo weather) {
        if (weather!=null){
            city.setText(weather.getCity());
            citynumber.setText(weather.getCitynumber());
            temp.setText(weather.getTemp());
            sd.setText(weather.getSd());
            wd.setText(weather.getWd());
            ws.setText(weather.getWs());
        }
        else {
            city.setText("未知");
        }
    }

    @Override
    public void onFail(String msg) {
        city.setText(msg);
    }
}
